import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
  {
    path:'/login',
    name:'login',
    component:()=>import('../views/Login/Login.vue')
  },
  {
    path:'/overtime',
    name:'overtime',
    component:()=>import('../views/OverTime/OverTime.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  //判断是否是已登录
  var token=localStorage.getItem('token')
  if(token){//如果是已登录
    if(to.path==='/login'){
        next('/home');
    }else{
      next();
    }
  }else{//未登录
    if(to.path==='/login'){
      next();
    }else{
      next('/login')
    }
  }
})
export default router
